package com.gov.ec.rsv.servicio;

import com.gov.ec.rsv.dao.CrudDAO;
import com.gov.ec.rsv.interfaces.IClienteServicio;
import com.gov.ec.rsv.modelo.Cliente;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.Query;
import org.apache.log4j.Logger;

/**
 *
 * @author 
 */
@Stateless
@LocalBean
public class ClienteServicio extends CrudDAO implements IClienteServicio
{
    /** La variable localLogger. */
    public static Logger localLogger = Logger.getLogger(ClienteServicio.class);
 
    @Override
	@SuppressWarnings("unchecked")
	public List<Cliente> listar() {
        return super.em.createQuery("select o from Cliente as o").setMaxResults(20).getResultList();
    }
    
   
    /* (non-Javadoc)
	 * @see com.gov.ec.rsv.servicio.IClienteServicio#conteo(java.lang.String)
	 */
   
    @Override
	public Integer conteo(final String nombre) {
        try {
            
            Query query = em.createQuery("select count(o) from Cliente as o "
                    + "where o.nombres like ?1" );
            query.setParameter(1,'%'+nombre+'%');
            return Integer.valueOf(query.getSingleResult().toString());
        } catch (Exception e) {
            //localLogger.error(e.getMessage(),e);
            return 0;
        }
    }
    
     /* (non-Javadoc)
	 * @see com.gov.ec.rsv.servicio.IClienteServicio#buscarCli(java.lang.String, int, int)
	 */
    @Override
	@SuppressWarnings("unchecked")
	public List<Cliente> buscarCli(final String nombre,
             final int maxResults, final int firstResult) {
        try {
            Query query = em.createQuery("select o from Cliente as o "
                    + "where o.nombres like ?1" );
            query.setParameter(1,'%'+nombre+'%');
            query.setMaxResults(maxResults);
            query.setFirstResult(firstResult);
             return query.getResultList();
        } catch (Exception e) {
            //localLogger.error(e.getMessage(), e);
            return new ArrayList<Cliente>();
        }
    }
    
    /* (non-Javadoc)
	 * @see com.gov.ec.rsv.servicio.IClienteServicio#buscarCliId(java.lang.String)
	 */
    
     @Override
	public Cliente buscarCliId(final String CI) {
        try {
        	
        	//EntityManagerFactory emf = Persistence.createEntityManagerFactory("RSVCCIGMEJB");
        	//em = emf.createEntityManager();
        	Query query =  em.createQuery("select o from Cliente as  o "
                    + "where o.cedula = ?1 " );
            query.setParameter(1,CI);
             return (Cliente)query.getSingleResult();
        } catch (Exception e) {
            //localLogger.error(e.getMessage(), e);
            return new Cliente();
        }
    }



   
 	
     
}
